package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.app.controller.teacher.vo.AppTeacherCreateReqVo;
import com.ruoyi.app.controller.teacher.vo.AppTeacherRespVo;
import com.ruoyi.app.controller.teacher.vo.AppTeacherUpdateReqVo;
import com.ruoyi.app.domain.AppTeacher;
import com.ruoyi.app.mapper.AppTeacherMapper;
import com.ruoyi.app.service.IAppTeacherService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 老师Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-11
 */
@Service
public class AppTeacherServiceImpl extends ServiceImpl<AppTeacherMapper, AppTeacher> implements IAppTeacherService {
    @Autowired
    private AppTeacherMapper appTeacherMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询老师
     *
     * @param id 老师主键
     * @return 老师
     */
    @Override
    public AppTeacher selectAppTeacherById(Long id) {
        AppTeacher entity = appTeacherMapper.selectById(id);
        return entity;
    }

    /**
     * 查询老师列表
     *
     * @param AppTeacher 老师
     * @return 老师
     */
    @Override
    public List<AppTeacherRespVo> selectAppTeacherList(AppTeacher appTeacher) {
        MPJLambdaWrapper<AppTeacher> lqw = new MPJLambdaWrapper<AppTeacher>()
                .selectAll(AppTeacher.class)
                .select(SysUser::getUserName, SysUser::getNickName,SysUser::getEmail,
                        SysUser::getPhonenumber,SysUser::getSex,SysUser::getAvatar,SysUser::getPassword)
                .leftJoin(SysUser.class,SysUser::getUserId,AppTeacher::getUserId)
                .likeIfExists(SysUser::getNickName,appTeacher.getNickName())
                ;

        return appTeacherMapper.selectJoinList(AppTeacherRespVo.class, lqw);
    }

    private LambdaQueryWrapper<AppTeacher> buildQueryWrapper(AppTeacher appTeacher) {
        LambdaQueryWrapper<AppTeacher> lqw = Wrappers.lambdaQuery();
        lqw.eq(appTeacher.getUserId() != null, AppTeacher::getUserId, appTeacher.getUserId());
        return lqw;
    }

    /**
     * 新增老师
     *
     * @param req 老师
     * @return 结果
     */
    @Override
    public int insertAppTeacher(AppTeacherCreateReqVo req) {
        //系统用户
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(req, sysUser);
        sysUser.setCreateBy(SecurityUtils.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(req.getPassword()));
        userMapper.insertUser(sysUser);

        //插入角色
        List<SysUserRole> list = new ArrayList<SysUserRole>(1);

        SysUserRole ur = new SysUserRole();
        ur.setUserId(sysUser.getUserId());
        ur.setRoleId(101L);
        list.add(ur);
        userRoleMapper.batchUserRole(list);


        AppTeacher appTeacher = new AppTeacher();
        appTeacher.setUserId(sysUser.getUserId());
        appTeacher.setNickName(sysUser.getNickName());
        return appTeacherMapper.insert(appTeacher);
    }

    /**
     * 修改老师
     *
     * @param req 老师
     * @return 结果
     */
    @Override
    public int updateAppTeacher(AppTeacherUpdateReqVo req) {
        SysUser user = userService.selectUserById(req.getUserId());
        BeanUtil.copyProperties(req, user);
        AppTeacher appTeacher = appTeacherMapper.selectOne(new LambdaQueryWrapper<AppTeacher>().eq(AppTeacher::getUserId, user.getUserId()));
        appTeacher.setNickName(user.getNickName());
        appTeacherMapper.updateById(appTeacher);
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除老师
     *
     * @param ids 需要删除的老师主键
     * @return 结果
     */
    @Override
    public int deleteAppTeacherByIds(Long[] ids) {
        return appTeacherMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老师信息
     *
     * @param id 老师主键
     * @return 结果
     */
    @Override
    public int deleteAppTeacherById(Long id) {
        return appTeacherMapper.deleteById(id);
    }
}
